#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#define sprite_vitesse 200
#define NUM_TRASH 2
#define MAX_SPEED 4



static SDL_Surface * ecran;
static SDL_Surface * trashs;


int main (int args, char * argv[] )
{
int i=0,face,nbrefrappe=0;

int continuer=1, tir = 0,cpt=0,bullt=0,relache=0,a=1;






  SDL_Init( SDL_INIT_EVERYTHING );
  SDL_Surface  * bg=NULL,* perso[4],*saut[8],*frappe[7],*jauge[15],*bullet[8],*trash=NULL,*death[6];

  ecran=SDL_SetVideoMode( 900,700 , 32 ,SDL_HWSURFACE );
  bg=IMG_Load("bgmehdi.jpg");
  SDL_Rect position;
  position.x=0;
  position.y=0;
  position.h=bg->h;
  position.w=bg->w=877;

trash=IMG_Load("Trash4.png");



perso[0]=IMG_Load("perso1.png");
perso[1]=IMG_Load("perso2.png");
perso[2]=IMG_Load("reverse.png");
perso[3]=IMG_Load("reverse2.png");



 SDL_Rect persoPosition;
 persoPosition.x=300;
 persoPosition.y=600;
 persoPosition.h=perso[0]->h;
 persoPosition.w=perso[0]->w;

trashs=IMG_Load("Trash1.png");


SDL_Rect trashposition;
trashposition.x=690;
trashposition.y=600;
trashposition.h=trashs->h;
trashposition.w=trashs->w;

SDL_Rect trashposition1;
trashposition1.x=10;
trashposition1.y=600;
trashposition1.h=trashs->h;
trashposition1.w=trashs->w;




saut[0]=IMG_Load("saut1.png");
saut[1]=IMG_Load("saut2.png");
saut[2]=IMG_Load("saut3.png");
saut[3]=IMG_Load("saut4.png");
saut[4]=IMG_Load("s1.png");
saut[5]=IMG_Load("s2.png");
saut[6]=IMG_Load("s3.png");
saut[7]=IMG_Load("s4.png");

death[0]=IMG_Load("death1.png");
death[1]=IMG_Load("death2.png");
death[2]=IMG_Load("death3.png");
death[3]=IMG_Load("death4.png");
death[4]=IMG_Load("death5.png");
death[5]=IMG_Load("death6.png");

frappe[0]=IMG_Load("waterman 1.png");
frappe[1]=IMG_Load("waterman 2.png");
frappe[2]=IMG_Load("waterman 3.png");
frappe[3]=IMG_Load("waterman 4.png");
frappe[4]=IMG_Load("r1.png");
frappe[5]=IMG_Load("r2.png");
frappe[6]=IMG_Load("r3.png");
frappe[7]=IMG_Load("r4.png");

jauge[0]=IMG_Load("1.jpg");
jauge[1]=IMG_Load("2.jpg");
jauge[2]=IMG_Load("3.jpg");
jauge[3]=IMG_Load("4.jpg");
jauge[4]=IMG_Load("5.jpg");
jauge[5]=IMG_Load("6.jpg");
jauge[6]=IMG_Load("7.jpg");
jauge[7]=IMG_Load("8.jpg");
jauge[8]=IMG_Load("9.jpg");
jauge[9]=IMG_Load("10.jpg");
jauge[10]=IMG_Load("11.jpg");
jauge[11]=IMG_Load("12.jpg");
jauge[12]=IMG_Load("13.jpg");
jauge[13]=IMG_Load("14.jpg");
jauge[14]=IMG_Load("15.jpg");
SDL_Rect jaugeposition;
jaugeposition.x=700;
jaugeposition.y=10;



bullet[0]=IMG_Load("b1.png");
bullet[1]=IMG_Load("b2.png");
bullet[2]=IMG_Load("b3.png");
bullet[3]=IMG_Load("b4.png");
bullet[4]=IMG_Load("b5.png");
bullet[5]=IMG_Load("b6.png");
bullet[6]=IMG_Load("b7.png");
bullet[7]=IMG_Load("b8.png");
SDL_Rect bulletposition;
bulletposition.x=300;
bulletposition.y=600;
bulletposition.w=bullet[0]->w;
bulletposition.h=bullet[0]->h;

SDL_Rect bulletposition1;
bulletposition1.x=300;
bulletposition1.y=600;
bulletposition1.w=bullet[0]->w;
bulletposition1.h=bullet[0]->h;


SDL_BlitSurface(jauge[0],NULL,ecran,&jaugeposition);

SDL_BlitSurface(bg,NULL,ecran,&position);



SDL_BlitSurface(trashs,NULL,ecran,&trashposition);











while(continuer)
{


if(trashposition.x<600)
a=1;
if(trashposition.x<10)
a=-1;
if(a==1)
trashposition.x+=50;
if(a==-1)
trashposition.x-=50;

SDL_BlitSurface(bg,NULL,ecran,&position);

SDL_BlitSurface(trashs,NULL,ecran,&trashposition);
SDL_BlitSurface(trashs,NULL,ecran,&trashposition1);
SDL_BlitSurface(perso[cpt],NULL,ecran,&persoPosition);
SDL_BlitSurface(jauge[i],NULL,ecran,&jaugeposition);





 SDL_Event event;
     SDL_PollEvent(&event);

if(event.type==SDL_QUIT)
{
break;
}







if(event.type==SDL_KEYDOWN)
{


if(event.key.keysym.sym==SDLK_ESCAPE)
{ continuer=0;
break ;


}

if(event.key.keysym.sym==SDLK_RIGHT)

{

persoPosition.x+=20;
face=1;
if(cpt>=1)
cpt=-1;
cpt++;
}

if(event.key.keysym.sym==SDLK_LEFT)

{

persoPosition.x-=20;
face=1;
if(cpt>=3)
cpt=1;
cpt++;
}

if(event.key.keysym.sym==SDLK_SPACE)

{

persoPosition.y-=50;



}



if(event.key.keysym.sym==SDLK_d)
{
bulletposition.x=persoPosition.x+persoPosition.w;

if(nbrefrappe>=3)
nbrefrappe=0;





nbrefrappe++;
SDL_Delay(70);
SDL_BlitSurface(bg,NULL,ecran,&position);

SDL_BlitSurface(trashs,NULL,ecran,&trashposition);
SDL_BlitSurface(trashs,NULL,ecran,&trashposition1);
SDL_BlitSurface(frappe[nbrefrappe],NULL,ecran,&persoPosition);
tir=1;
}




if(event.key.keysym.sym==SDLK_q)
{
bulletposition.x=persoPosition.x-persoPosition.w;


if(nbrefrappe>=6)
nbrefrappe=3;





nbrefrappe++;
SDL_Delay(70);
SDL_BlitSurface(bg,NULL,ecran,&position);

SDL_BlitSurface(trashs,NULL,ecran,&trashposition);
SDL_BlitSurface(trashs,NULL,ecran,&trashposition1);
SDL_BlitSurface(frappe[nbrefrappe],NULL,ecran,&persoPosition);
tir=2;







}












}









if(tir==1 && bulletposition.x!=trashposition1.x)
{

bulletposition.x+=50;
bulletposition1.x+=40;
SDL_BlitSurface(bullet[bullt],NULL,ecran,&bulletposition);

if(bullt>=7)
bullt=4;
bullt++;
SDL_Delay(70);
}




if(tir==2 && bulletposition.x!=trashposition1.x)
{

bulletposition.x-=50;
SDL_BlitSurface(bullet[bullt],NULL,ecran,&bulletposition);
if(bullt>=3)
bullt=0;
bullt++;
SDL_Delay(70);

}



if(trashposition.x==persoPosition.x)
{



i++;

}






  SDL_Flip(ecran);



}



SDL_FreeSurface(death[0]);
SDL_FreeSurface(trash);
SDL_FreeSurface(frappe[0]);
SDL_FreeSurface(trashs);
SDL_FreeSurface(perso[0]);
SDL_FreeSurface(bg);
SDL_FreeSurface(jauge[0]);
SDL_FreeSurface(saut[0]);
SDL_Quit();
return 0 ;



}

